home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / cc-mode / cc-menus.el.z / cc-menus.el
Encoding:
Text File  |  1998-05-21  |  13.7 KB  |  390 lines

  1. ;;; cc-menus.el --- imenu support for CC Mode
  2.  
  3. ;; Copyright (C) 1985,87,92,93,94,95,96,97,98 Free Software Foundation, Inc.
  4.  
  5. ;; Authors:    1992-1997 Barry A. Warsaw
  6. ;;             1987 Dave Detlefs and Stewart Clamen
  7. ;;             1985 Richard M. Stallman
  8. ;; Maintainer: cc-mode-help@python.org
  9. ;; Created:    22-Apr-1997 (split from cc-mode.el)
  10. ;; Version:    See cc-mode.el
  11. ;; Keywords:   c languages oop
  12.  
  13. ;; This file is part of GNU Emacs.
  14.  
  15. ;; GNU Emacs is free software; you can redistribute it and/or modify
  16. ;; it under the terms of the GNU General Public License as published by
  17. ;; the Free Software Foundation; either version 2, or (at your option)
  18. ;; any later version.
  19.  
  20. ;; GNU Emacs is distributed in the hope that it will be useful,
  21. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23. ;; GNU General Public License for more details.
  24.  
  25. ;; You should have received a copy of the GNU General Public License
  26. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  27. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  28. ;; Boston, MA 02111-1307, USA.
  29.  
  30.  
  31. ;; imenu integration
  32. (defvar cc-imenu-c-prototype-macro-regexp nil
  33.   "RE matching macro names used to conditionally specify function prototypes.
  34.  
  35. For example:
  36.  
  37.     #ifdef __STDC__
  38.       #define _P(x) x
  39.     #else
  40.       #define _P(x) /*nothing*/
  41.     #endif
  42.  
  43.     int main _P( (int argc, char *argv[]) )
  44.  
  45. A sample value might look like: `\\(_P\\|_PROTO\\)'.")
  46.  
  47. (defvar cc-imenu-c++-generic-expression
  48.   (` 
  49.    (
  50.     ;; Try to match ::operator definitions first. Otherwise `X::operator new ()'
  51.     ;; will be incorrectly recognised as function `new ()' because the regexps
  52.     ;; work by backtracking from the end of the definition.
  53.     (nil
  54.      (, 
  55.       (concat
  56.        "^\\<.*"
  57.        "[^a-zA-Z0-9_:<>~]"                    ; match any non-identifier char
  58.                                               ; (note: this can be `\n')
  59.        "\\("
  60.           "\\([a-zA-Z0-9_:<>~]*::\\)?"        ; match an operator
  61.           "operator\\>[ \t]*"
  62.           "\\(()\\|[^(]*\\)"                  ; special case for `()' operator
  63.        "\\)"
  64.  
  65.        "[ \t]*([^)]*)[ \t]*[^ \t;]"           ; followed by ws, arg list,
  66.                                               ; require something other than
  67.                                               ; a `;' after the (...) to
  68.                                               ; avoid prototypes.  Can't
  69.                                               ; catch cases with () inside
  70.                                               ; the parentheses surrounding
  71.                                               ; the parameters.  e.g.:
  72.                                               ; `int foo(int a=bar()) {...}'
  73.        )) 1)
  74.     ;; Special case to match a line like `main() {}'
  75.     ;; e.g. no return type, not even on the previous line.
  76.     (nil
  77.      (, 
  78.       (concat
  79.        "^"
  80.        "\\([a-zA-Z_][a-zA-Z0-9_:<>~]*\\)"     ; match function name
  81.        "[ \t]*("                  ; see above, BUT
  82.        "[ \t]*\\([^ \t(*][^)]*\\)?)"          ; the arg list must not start
  83.        "[ \t]*[^ \t;(]"                       ; with an asterisk or parentheses
  84.        )) 1)
  85.     ;; General function name regexp
  86.     (nil
  87.      (, 
  88.       (concat
  89.        "^\\<.*"                               ; line MUST start with word char
  90.        "[^a-zA-Z0-9_:<>~]"                    ; match any non-identifier char
  91.        "\\([a-zA-Z_][a-zA-Z0-9_:<>~]*\\)"     ; match function name
  92.        "[ \t]*("                  ; see above, BUT
  93.        "[ \t]*\\([^ \t(*][^)]*\\)?)"          ; the arg list must not start
  94.        "[ \t]*[^ \t;(]"                       ; with an asterisk or parentheses
  95.        )) 1)
  96.     ;; Special case for definitions using phony prototype macros like:
  97.     ;; `int main _PROTO( (int argc,char *argv[]) )'.
  98.     ;; This case is only included if cc-imenu-c-prototype-macro-regexp is set.
  99.     ;; Only supported in c-code, so no `:<>~' chars in function name!
  100.     (,@ (if cc-imenu-c-prototype-macro-regexp
  101.             (` ((nil
  102.                  (,
  103.                   (concat
  104.                    "^\\<.*"                   ; line MUST start with word char
  105.                    "[^a-zA-Z0-9_]"            ; match any non-identifier char
  106.                    "\\([a-zA-Z_][a-zA-Z0-9_]*\\)"       ; match function name
  107.                    "[ \t]*"                   ; whitespace before macro name
  108.                    cc-imenu-c-prototype-macro-regexp
  109.                    "[ \t]*("                  ; ws followed by first paren.
  110.                    "[ \t]*([^)]*)[ \t]*)[ \t]*[^ \t;]" ; see above
  111.                    )) 1)))))
  112.     ;; Class definitions
  113.     ("Class" 
  114.      (, (concat 
  115.          "^"                                  ; beginning of line is required
  116.          "\\(template[ \t]*<[^>]+>[ \t]*\\)?" ; there may be a `template <...>'
  117.          "class[ \t]+"
  118.          "\\([a-zA-Z0-9_]+\\)"                ; the string we want to get
  119.          "[ \t]*[:{]"
  120.          )) 2)))
  121.   "Imenu generic expression for C++ mode.  See `imenu-generic-expression'.")
  122.  
  123. (defvar cc-imenu-c-generic-expression
  124.   cc-imenu-c++-generic-expression
  125.   "Imenu generic expression for C mode.  See `imenu-generic-expression'.")
  126.  
  127. (defvar cc-imenu-java-generic-expression
  128.   (`
  129.    ((nil
  130.      (,
  131.       (concat
  132.        "^\\([ \t]\\)*"
  133.        "\\([A-Za-z0-9_-]+[ \t]+\\)?"          ; type specs; there can be
  134.         "\\([A-Za-z0-9_-]+[ \t]+\\)?"          ; more than 3 tokens, right?
  135.        "\\([A-Za-z0-9_-]+[ \t]*[[]?[]]?\\)"
  136.        "\\([ \t]\\)"
  137.        "\\([A-Za-z0-9_-]+\\)"              ; the string we want to get
  138.        "\\([ \t]*\\)+("
  139.        "\\([a-zA-Z,_1-9\n \t]*[[]?[]]?\\)*"   ; arguments
  140.        ")[ \t]*"
  141. ;       "[^;(]"
  142.        "[,a-zA-Z_1-9\n \t]*{"               
  143.        )) 6)))
  144.   "Imenu generic expression for Java mode.  See `imenu-generic-expression'.")
  145.  
  146. ;;                        *Warning for cc-mode developers* 
  147. ;;
  148. ;; `cc-imenu-objc-generic-expression' elements depend on
  149. ;; `cc-imenu-c++-generic-expression'. So if you change this
  150. ;; expression, you need to change following variables,
  151. ;; `cc-imenu-objc-generic-expression-*-index',
  152. ;; too. `cc-imenu-objc-function' uses these *-index variables, in
  153. ;; order to know where the each regexp *group \\(foobar\\)* elements
  154. ;; are started.
  155. ;;
  156. ;; *-index variables are initialized during `cc-imenu-objc-generic-expression' 
  157. ;; being initialized. 
  158. ;;
  159.  
  160. ;; Internal variables
  161. (defvar cc-imenu-objc-generic-expression-noreturn-index nil)
  162. (defvar cc-imenu-objc-generic-expression-general-func-index nil)
  163. (defvar cc-imenu-objc-generic-expression-proto-index nil)
  164. (defvar cc-imenu-objc-generic-expression-objc-base-index nil)
  165.  
  166. (defvar cc-imenu-objc-generic-expression 
  167.   (concat 
  168.    ;;
  169.    ;; For C 
  170.    ;;
  171.    ;; > Special case to match a line like `main() {}'
  172.    ;; > e.g. no return type, not even on the previous line.
  173.    ;; Pick a token by (match-string 1)
  174.    (car (cdr (nth 1 cc-imenu-c++-generic-expression))) ; -> index += 2
  175.    (prog2 (setq cc-imenu-objc-generic-expression-noreturn-index 1) "")
  176.    "\\|"
  177.    ;; > General function name regexp
  178.    ;; Pick a token by  (match-string 3)
  179.    (car (cdr (nth 2 cc-imenu-c++-generic-expression))) ; -> index += 2
  180.    (prog2 (setq cc-imenu-objc-generic-expression-general-func-index 3) "")
  181.    ;; > Special case for definitions using phony prototype macros like:
  182.    ;; > `int main _PROTO( (int argc,char *argv[]) )'.
  183.    ;; Pick a token by  (match-string 5)
  184.    (if cc-imenu-c-prototype-macro-regexp
  185.        (concat    
  186.     "\\|"
  187.     (car (cdr (nth 3 cc-imenu-c++-generic-expression))) ; -> index += 1
  188.     (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index 6) "")
  189.     )
  190.      (prog2 (setq cc-imenu-objc-generic-expression-objc-base-index 5) "")
  191.      "")                ; -> index += 0
  192.    (prog2 (setq cc-imenu-objc-generic-expression-proto-index 5) "")
  193.    ;;
  194.    ;; For Objective-C
  195.    ;; Pick a token by (match-string 5 or 6)
  196.    ;;
  197.    "\\|\\("                         
  198.    "^[-+][:a-zA-Z0-9()*_<>\n\t ]*[;{]"        ; Methods
  199.    "\\|" 
  200.    "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*:"  
  201.    "\\|" 
  202.    "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*([a-zA-Z0-9_]+)"
  203.    "\\|" 
  204.    ;; For NSObject, NSProxy and Object... They don't have super class.
  205.    "^@interface[\t ]+[a-zA-Z0-9_]+[\t ]*.*$"
  206.    "\\|" 
  207.    "^@implementation[\t ]+[a-zA-Z0-9_]+[\t ]*([a-zA-Z0-9_]+)"
  208.    "\\|" 
  209.    "^@implementation[\t ]+[a-zA-Z0-9_]+"
  210.    "\\|" 
  211.    "^@protocol[\t ]+[a-zA-Z0-9_]+" "\\)")
  212.   "Imenu generic expression for ObjC mode.  See `imenu-generic-expression'.")
  213.  
  214.  
  215. ;; Imenu support for objective-c uses functions.
  216. (defsubst cc-imenu-objc-method-to-selector (method)
  217.   "Return the objc selector style string of METHOD.
  218. Example: 
  219. - perform: (SEL)aSelector withObject: object1 withObject: object2; /* METHOD */
  220. =>
  221. -perform:withObject:withObject:withObject: /* selector */"
  222.   (let ((return "")            ; String to be returned
  223.     (p 0)                ; Current scanning position in METHOD  
  224.     (pmax (length method))        ; 
  225.     char                ; Current scanning target
  226.     (betweenparen 0)        ; CHAR is in parentheses.
  227.     argreq                ; An argument is required.
  228.     inargvar)            ; position of CHAR is in an argument variable.
  229.     (while (< p pmax)
  230.       (setq char (aref method p)
  231.         p (1+ p))
  232.       (cond
  233.        ;; Is CHAR part of a objc token?
  234.        ((and (not inargvar)     ; Ignore if CHAR is part of an argument variable.
  235.          (eq 0 betweenparen) ; Ignore if CHAR is in parentheses.
  236.          (or (and (<= ?a char) (<= char ?z))
  237.          (and (<= ?A char) (<= char ?Z))
  238.          (and (<= ?0 char) (<= char ?9))
  239.          (= ?_ char)))
  240.     (if argreq    
  241.         (setq inargvar t
  242.           argreq nil)
  243.       (setq return (concat return (char-to-string char)))))
  244.        ;; Or a white space?
  245.        ((and inargvar (or (eq ?\  char) (eq ?\n char)) 
  246.          (setq inargvar nil)))
  247.        ;; Or a method separator?
  248.        ;; If a method separator, the next token will be an argument variable.
  249.        ((eq ?: char)            
  250.     (setq argreq t            
  251.           return (concat return (char-to-string char))))
  252.        ;; Or an open parentheses?
  253.        ((eq ?\( char)
  254.     (setq betweenparen (1+ betweenparen)))
  255.        ;; Or a close parentheses?
  256.        ((eq ?\) char)
  257.     (setq betweenparen (1- betweenparen)))))
  258.     return))
  259.  
  260. (defun cc-imenu-objc-remove-white-space  (str)
  261.   "Remove all spaces and tabs from STR."
  262.   (let ((return "")
  263.     (p 0)
  264.     (max (length str)) 
  265.     char)
  266.     (while (< p max)
  267.       (setq char (aref str p))
  268.       (setq p (1+ p))
  269.       (if (or (= char ?\ ) (= char ?\t))
  270.       ()
  271.     (setq return (concat return (char-to-string char)))))
  272.     return))
  273.  
  274. (defun cc-imenu-objc-function ()
  275.   "imenu supports for objc-mode."
  276.   (let (methodlist
  277.     clist
  278.     ;;
  279.     ;; OBJC, Cnoreturn, Cgeneralfunc, Cproto are constants.
  280.     ;;
  281.     ;;                  *Warning for developers* 
  282.     ;; These constants depend on `cc-imenu-c++-generic-expression'.
  283.     ;;
  284.     (OBJC cc-imenu-objc-generic-expression-objc-base-index)
  285.     ;; Special case to match a line like `main() {}'
  286.     (Cnoreturn cc-imenu-objc-generic-expression-noreturn-index)
  287.     ;; General function name regexp
  288.     (Cgeneralfunc cc-imenu-objc-generic-expression-general-func-index)
  289.     ;; Special case for definitions using phony prototype macros like:
  290.     (Cproto cc-imenu-objc-generic-expression-proto-index)
  291.     langnum
  292.     ;;
  293.     (classcount 0)
  294.     toplist
  295.     stupid
  296.     str
  297.     str2 
  298.     (intflen (length "@interface"))
  299.     (implen  (length "@implementation"))
  300.     (prtlen  (length "@protocol"))
  301.     (func
  302.      ;;
  303.      ;; Does this emacs has buffer-substring-no-properties? 
  304.      ;;
  305.      (if (fboundp 'buffer-substring-no-properties)
  306.          'buffer-substring-no-properties
  307.        'buffer-substring)))
  308.     (goto-char (point-max))
  309.     (imenu-progress-message stupid 0)
  310.     ;;
  311.     (while (re-search-backward cc-imenu-objc-generic-expression nil t)
  312.       (imenu-progress-message stupid)
  313.       (setq langnum (if (match-beginning OBJC) 
  314.             OBJC
  315.               (cond
  316.                ((match-beginning Cproto) Cproto)
  317.                ((match-beginning Cgeneralfunc) Cgeneralfunc)
  318.                ((match-beginning Cnoreturn) Cnoreturn))))
  319.       (setq str (funcall func (match-beginning langnum) (match-end langnum)))
  320.       ;;
  321.       (cond 
  322.        ;;
  323.        ;; C
  324.        ;;
  325.        ((not (eq langnum OBJC))
  326.     (setq clist (cons (cons str (match-beginning langnum)) clist)))
  327.        ;;
  328.        ;; ObjC
  329.        ;; 
  330.        ;; An instance Method
  331.        ((eq (aref str 0) ?-)
  332.     (setq str (concat "-" (cc-imenu-objc-method-to-selector str)))
  333.     (setq methodlist (cons (cons str
  334.                   (match-beginning langnum))
  335.             methodlist)))
  336.        ;; A factory Method
  337.        ((eq (aref str 0) ?+)
  338.     (setq str (concat "+" (cc-imenu-objc-method-to-selector str)))
  339.     (setq methodlist (cons (cons str
  340.                   (match-beginning langnum))
  341.             methodlist)))
  342.        ;; Interface or implementation or protocol 
  343.        ((eq (aref str 0) ?@)
  344.     (setq classcount (1+ classcount))
  345.     (cond 
  346.      ((and (> (length str) implen)
  347.            (string= (substring  str 0 implen) "@implementation"))
  348.       (setq str (substring str implen)
  349.         str2 "@implementation"))
  350.      ((string= (substring  str 0 intflen) "@interface")
  351.       (setq str (substring str intflen)
  352.         str2 "@interface"))
  353.      ((string= (substring  str 0 prtlen) "@protocol")
  354.       (setq str (substring str prtlen)
  355.         str2 "@protocol")))
  356.     (setq str (cc-imenu-objc-remove-white-space str))
  357.     (setq methodlist (cons (cons str2
  358.                   (match-beginning langnum))
  359.                    methodlist))
  360.     (setq toplist (cons nil (cons (cons str
  361.                       methodlist) toplist))
  362.           methodlist nil))))
  363.     ;; 
  364.     (imenu-progress-message stupid 100)
  365.     (if (eq (car toplist) nil)
  366.     (setq toplist (cdr toplist)))
  367.  
  368.     ;; In this buffer, there is only one or zero @{interface|implementation|protocol}.
  369.     (if (< classcount 2)
  370.     (let ((classname (car (car toplist)))
  371.           (p (cdr (car (cdr (car toplist)))))
  372.           last)
  373.       (setq toplist (cons (cons classname p) (cdr (cdr (car toplist)))))
  374.       ;; Add C lang token
  375.       (if clist
  376.           (progn
  377.         (setq last toplist)
  378.         (while (cdr last)
  379.           (setq last (cdr last)))
  380.         (setcdr last clist))))
  381.       ;; Add C lang tokens as a sub menu
  382.       (setq toplist (cons (cons "C" clist) toplist)))
  383.     ;;
  384.     toplist
  385.     ))
  386.  
  387.  
  388. (provide 'cc-menus)
  389. ;;; cc-menus.el ends here
  390.